#define UNIT_TESTING
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "find_sub_str.c"  // 包含头文件

// 测试正常情况
static void test_find_substring_normal(void **state) {
    (void) state;  // 未使用

    char str[] = "bbccaadd";
    char substr[] = "aa";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, 4);
}

// 测试子串在开头的情况
static void test_find_substring_at_start(void **state) {
    (void) state;

    char str[] = "aabbccdd";
    char substr[] = "aa";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, 0);
}

// 测试子串在结尾的情况
static void test_find_substring_at_end(void **state) {
    (void) state;

    char str[] = "bbccddaa";
    char substr[] = "aa";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, 6);
}

// 测试未找到子串的情况
static void test_find_substring_not_found(void **state) {
    (void) state;

    char str[] = "bbccdd";
    char substr[] = "aa";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, -1);
}

// 测试空子串的情况
static void test_find_substring_empty_substr(void **state) {
    (void) state;

    char str[] = "bbccdd";
    char substr[] = "";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, -1);
}

// 测试子串比原字符串长的情况
static void test_find_substring_longer_substr(void **state) {
    (void) state;

    char str[] = "bbccdd";
    char substr[] = "bbccddee";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, -1);
}

// 测试大小写敏感
static void test_find_substring_case_sensitive(void **state) {
    (void) state;

    char str[] = "bbccAAdd";
    char substr[] = "aa";
    int result = find_substring(str, strlen(str), substr);
    assert_int_equal(result, -1);
}

int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(test_find_substring_normal),
        cmocka_unit_test(test_find_substring_at_start),
        cmocka_unit_test(test_find_substring_at_end),
        cmocka_unit_test(test_find_substring_not_found),
        cmocka_unit_test(test_find_substring_empty_substr),
        cmocka_unit_test(test_find_substring_longer_substr),
        cmocka_unit_test(test_find_substring_case_sensitive),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}